/*
This file is part of Docking@Home.
http://docking.gcl.cis.udel.edu
Copyright (C) 2009 University of Delaware

Docking@Home is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Docking@Home is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Docking@Home.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Filename: OBJModel.cpp
	Description: Handles all the desired functionality required
		from an OBJ Model
	Revision History: 
		Created - 01/25/08
	Author: Robert Keller (01/25/08 -> Current)
*/

#include "OBJModel.h"

#include "parse.h"
#include "gutil.h"
#include "boinc_gl.h"
#include "graphics_api.h"
#include "txf_util.h"

// ------------------------------------------------------------
// Constructor / Destructor
// ------------------------------------------------------------

OBJModel::OBJModel ()
{
}// OBJModel

OBJModel::~OBJModel ()
{
}// ~OBJModel

// ------------------------------------------------------------
// Loading Model Functions
// ------------------------------------------------------------

void OBJModel::loadModel (char objFilename[OBJ_MAX_PATH_LENGTH])
{
	FILE *objFile; // pointer to obj file

	char obj_resolved_name[256];

	char line[100]; // One line of text
	char *token; // String token
	OBJVertex tempVertex;

	// resolve .obj file
	if (boinc_resolve_filename(objFilename, 
		obj_resolved_name, sizeof(obj_resolved_name)))
	{
		fprintf(stderr, "Error resolving obj file.\n");
	}// if

	objFile = fopen(obj_resolved_name, "r"); 

	// Read a line at a time until none exist
	while(fgets(line, 100, objFile) != NULL)
	{
		token = strtok(line, " ");

		// Vertex
		if (token[0] == 'v')
		{
			// X Coordinate
			token = strtok(NULL, " ");
			tempVertex.x = (float)atof(token);
			
			// Y Coordinate
			token = strtok(NULL, " ");
			tempVertex.y = (float)atof(token);

			// Z Coordinate
			token = strtok(NULL, " ");
			tempVertex.z = (float)atof(token);

			vertices.push_back(tempVertex);
		}// if
		// Face
		else if (token[0] == 'f')
		{
			// Face index
			token = strtok(NULL, " ");

			while (token != NULL)
			{
				// Add an index
				atoi(token);

				// Face index
				token = strtok(NULL, " ");
			}// while
		}// else if
	}// while

}// loadModel

// ------------------------------------------------------------
// Private Loading Model Functions
// ------------------------------------------------------------

void OBJModel::generateNormals ()
{

}// generateNormals

// ------------------------------------------------------------
// Clean Model (remove old data)
// ------------------------------------------------------------

void OBJModel::clean ()
{
	vertices.clear();
}// clean